from nltk.corpus import wordnet as wn
from nltk.corpus import verbnet as vn
import nltk
import sys

from WnGoogle import WnGoogle
from WnDbAdapter import WnDbAdapter
from WnParser import WnParser
from nltk.stem.porter import *
from nltk.corpus import brown
from nltk import pos_tag, word_tokenize
from WnSimilarity import WnSimilarity
from WnStemmer import WnStemmer



GOOGLE_KEY = "010258595658265763838:yjugfmnheb4"
GOOGLE_SECRET = "AIzaSyB8jF0ZRlikfoC6QXOCRuk6F7PqVEvIymQ"

try:
	if len(sys.argv) > 1:
		if sys.argv[1] == "get_results" and len(sys.argv)>2:
			keywords = sys.argv[2:]
			for keyword in keywords:
				g = WnGoogle(GOOGLE_KEY, GOOGLE_SECRET)
				g.setDbAdapter(WnDbAdapter("localhost", "root", "ind12*3", "seo"))
				g.setKeyword(keyword)
				g.getResults()
				g.saveResults()
		if sys.argv[1] == "calculate" and len(sys.argv)>2:
			keywords = sys.argv[2:]
			db = WnDbAdapter("localhost", "root", "ind12*3", "seo");
			stemmer = WnStemmer()
			similarity = WnSimilarity()

			features = { "description", "title", "mobilized" }
			similarity_methods = { "lch", "lin", "wup", "jcn" }
			#features = { "mobilized" }
			#similarity_methods = { "lch" }
			
			# Calculating similarity
			for keyword in keywords:
				rows = db.getKeywordResults(keyword);
				similarity.setKeyword(keyword)
				for row in rows:
					for feature in features:
						for similarity_method in similarity_methods:
							total_similarity = 0
							
							if row.has_key(feature):
								text = row[feature]
							else: 
								raise Exception ('row', 'No such column: %s'%feature)

							
							stemmer.prepareText(text)
							words = stemmer.getWords()
							
							total_words = 0
							for word in words:
								sim = 0
								sim = similarity.calcLch(word, similarity_method)
								if sim > 0.4:
									total_similarity+=sim
									total_words+=1
							if len(words)>0:
								total_similarity = total_similarity / len(words)
							print "%s\t[%d]\t%f\t%f"%(feature,row['position'],total_words,total_similarity)
							db.setColumn(keyword, row['position'], similarity_method+"_"+feature, total_similarity)
	else:
		raise Exception('no arguments', 'no arguments')

except Exception as e:
	print e
	print """Usage: wordnet.py command options
	Commands:
		get_results keyword - fetches results from google and stores it in database
		calculate keyword - calculates similarity by keyword
	"""


#corp = nltk.Text(word.lower() for word in nltk.corpus.brown.words())
#corp.similar('skis')



#classes = vn.classids('drink')
#print vn.wordnetids(classes[0])

#dog = wn.synsets('beer')
#print dog
#for i in dog:
#	print i.verb_groups()
#dog = wn.synset('dog.n.01');
#cat = wn.synset('cat.n.01');
##print dog.path_similarity(cat)
#print dog.lch_similarity(cat)
#print dog.wup_similarity(cat)

import urllib
import simplejson

from lxml import etree

class WnSimilarityChecker:

	def __init__(self):
		print "asd"











			#print "Similarity %s=>%s = %f"%("furniture", word,sim)
	#print total_similarity
	


#corp = nltk.Text(word.lower() for word in nltk.corpus.brown.words())
#corp.similar('skis')



#classes = vn.classids('drink')
#print vn.wordnetids(classes[0])

#dog = wn.synsets('beer')
#print dog
#for i in dog:
#	print i.verb_groups()
#dog = wn.synset('dog.n.01');
#cat = wn.synset('cat.n.01');
##print dog.path_similarity(cat)
#print dog.lch_similarity(cat)
#print dog.wup_similarity(cat)


#print p.getH1();

#
#g.parseResults()

#query = "furniture"
#url = 'https://www.googleapis.com/customsearch/v1?key=AIzaSyB8jF0ZRlikfoC6QXOCRuk6F7PqVEvIymQ&cx=010258595658265763838:yjugfmnheb4&q=%s&gl=ca' % query
#sr = urllib.urlopen(url)
#json = simplejson.loads(sr.read())
#results = json['items']
#for i in results:#
#	print '---'
#	print 'title:', i['title']
#	print 'url:', i['link'], '\n'





#url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=furniture"
#search_results = urllib.urlopen(url)
#json = simplejson.loads(search_results.read())
#results = json["responseData"]["results"]
#for i in results:
#	print i["title"] + ": " + i["url"]



		